﻿Public Class frmPhanCong

    Sub LoadDanhSach()
        Dim _pc As New DataTable
        _pc = PhanCongDAO.SelectAll()
        dgvTTPhanCong.DataSource = _pc
    End Sub

    Sub Display()
        If (dgvTTPhanCong.SelectedRows.Count > 0) Then
            TXTMAPC.Text = dgvTTPhanCong.SelectedRows(0).Cells(0).Value.ToString()
            cmbHOTEN.SelectedValue = dgvTTPhanCong.SelectedRows(0).Cells(1).Value.ToString()
            dtpNGAYDAY.Value = dgvTTPhanCong.SelectedRows(0).Cells(2).Value
            dtpGIODAY.Value = dgvTTPhanCong.SelectedRows(0).Cells(3).Value.ToString()
            cmbSOTIET.Text = dgvTTPhanCong.SelectedRows(0).Cells(4).Value.ToString()
            cmbTENLOP.SelectedValue = dgvTTPhanCong.SelectedRows(0).Cells(5).Value.ToString()
            cmbTENPHONG.SelectedValue = dgvTTPhanCong.SelectedRows(0).Cells(6).Value.ToString()
        End If
    End Sub

    Private Sub frmPhanCong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbSOTIET.SelectedIndex = 4

        Dim _gv As New DataTable
        _gv = GiaoVienDAO.SelectAll()
        cmbHOTEN.DataSource = _gv
        cmbHOTEN.DisplayMember = "HoTen"
        cmbHOTEN.ValueMember = "MaGV"

        Dim _ph As New DataTable
        _ph = PhongHocDAO.SelectAll()
        cmbTENPHONG.DataSource = _ph
        cmbTENPHONG.DisplayMember = "TenPhong"
        cmbTENPHONG.ValueMember = "MaPhong"

        Dim _lh As New DataTable
        _lh = LopHocDAO.SelectAll()
        cmbTENLOP.DataSource = _lh
        cmbTENLOP.DisplayMember = "TenLop"
        cmbTENLOP.ValueMember = "MaLop"

        dgvTTPhanCong.Rows.Clear()
        LoadDanhSach()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        Dim _pc As PhanCongDTO = New PhanCongDTO()
        _pc.MaGV = cmbHOTEN.SelectedValue
        _pc.NgayDay = dtpNGAYDAY.Text
        _pc.GioDay = dtpGIODAY.Text
        _pc.SoTiet = cmbSOTIET.Text
        _pc.MaPhong = cmbTENPHONG.SelectedValue
        _pc.MaLop = cmbTENLOP.SelectedValue
        Try
            PhanCongDAO.Insert(_pc)
            MessageBox.Show("Đã thêm thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub dgvTTPhanCong_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTPhanCong.CellContentClick
        Display()
    End Sub

    Private Sub dgvTTPhanCong_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTPhanCong.SelectionChanged
        Display()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTPhanCong.SelectedRows.Count > 0) Then
            Dim _pc As PhanCongDTO = New PhanCongDTO()
            _pc.MaPhanCong = TXTMAPC.Text
            _pc.MaGV = cmbHOTEN.SelectedValue
            _pc.NgayDay = dtpNGAYDAY.Text
            _pc.GioDay = dtpGIODAY.Text
            _pc.SoTiet = cmbSOTIET.Text
            _pc.MaPhong = cmbTENPHONG.SelectedValue
            _pc.MaLop = cmbTENLOP.SelectedValue
            Try
                PhanCongDAO.Update(_pc)
                MessageBox.Show("Đã cập nhật thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _pc As PhanCongDTO = New PhanCongDTO()
        _pc.MaPhanCong = TXTMAPC.Text
        Try
            PhanCongDAO.Delete(_pc)
            MessageBox.Show("Đã xóa dữ liệu thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub frmPhanCong_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTPhanCong.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _pc As PhanCongDTO = New PhanCongDTO()
                _pc.MaPhanCong = TXTMAPC.Text
                _pc.MaGV = cmbHOTEN.SelectedValue
                _pc.NgayDay = dtpNGAYDAY.Value
                _pc.GioDay = dtpGIODAY.Text
                _pc.SoTiet = cmbSOTIET.Text
                _pc.MaPhong = cmbTENPHONG.SelectedValue
                _pc.MaLop = cmbTENLOP.SelectedValue
                Try
                    PhanCongDAO.Update(_pc)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub btnIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIN.Click
        Dim _dt As New DataTable
        _dt.Clear()
        _dt = PhanCongDAO.SelectAll()
        dgvTTPhanCong.DataSource = _dt
        'Nap du lieu cho Report
        Dim myReport As New CR_PhanCong()
        myReport.SetDataSource(_dt.DataSet)
        'Nap form in report
        Dim f As New InPhanCong
        f.CRV_PhanCong.ReportSource = myReport
        f.ShowDialog()
    End Sub
End Class